package com.demo.dune.service.strategy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.demo.dune.base.ServiceErrorException;
import com.demo.dune.domain.MemCreateInput;
import com.demo.dune.domain.MemberVO;
import com.demo.dune.service.MemberService;
import lombok.extern.slf4j.Slf4j;

/**
 * SilverMemberStrategy for SilverMember.
 * @author Allen
 * @version 1.0 2022-04-05
 */
@Slf4j
@Component
public class SilverMemberStrategy implements IMemberStrategy {
	
	public static final String SILVER_MEM_CODE_PREFIX = "SM_";
	
    @Autowired
    MemberService memberService;

	@Override
	public String getName() {
		return MemberStrategyConst.SILVER_MEM_STRATEGY;
	}

	@Override
	public void createMem(MemCreateInput memInput) {
		try {
			log.info("run SilverMemberStrategy to create");
			if (memInput == null) {
				throw new ServiceErrorException("SilverMemberStrategy null memInput Exception");
			}
			MemberVO member = new MemberVO();
			member.setCode(SILVER_MEM_CODE_PREFIX + memInput.getName());
			member.setName(memInput.getName());
			member.setType(MemberTypeEnum.SILVER.getCode());
			member.setPoint(0L);
			memberService.save(member);
		} catch (Exception e) {
			log.error("SilverMemberStrategy occur error.");
			throw new ServiceErrorException("SilverMemberStrategy create Exception", e);
		}
		log.info("SilverMemberStrategy create success");
	}

	@Override
	public long calcPoint(long originalPrice) {
		return originalPrice * 2;
	}

}
